package com.newenergy.wisehome.update;


import android.app.IntentService;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.newenergy.wisehome.common.MyEvent;
import com.newenergy.wisehome.common.helper.SPUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import static com.newenergy.wisehome.config.Constants.ISDOWNLOAD;

/**
 * Created by Crazyfzw on 2016/8/21.
 * 创建服务完成apk文件的下载，下载完成后调用系统的安装程序完成安装
 */
public class DownloadApkService extends IntentService {

    private static final int BUFFER_SIZE = 10 * 1024; //缓存大小
    private static final String TAG = "DownloadService";

    private static final int NOTIFICATION_ID = 0;
    private NotificationManager mNotifyManager;
    private NotificationCompat.Builder mBuilder;
    private String mUrlStr;
    private String mId="1";

    public DownloadApkService() {
        super("DownloadApkService");
    }



    /**
     * 在onHandleIntent中下载apk文件
     * @param intent
     */
    @Override
    protected void onHandleIntent(Intent intent) {
//        SPUtil.put(ISDOWNLOAD,true);
        //初始化通知，用于显示下载进度
        mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel("1","Channel1", NotificationManager.IMPORTANCE_LOW);
            mNotifyManager.createNotificationChannel(channel);
        }

        mBuilder = new NotificationCompat.Builder(this,mId);
        mBuilder.setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE);
        mBuilder.setVibrate(new long[]{0});
        mBuilder.setSound(null);
        String appName = getString(getApplicationInfo().labelRes);
        int icon = getApplicationInfo().icon;
        mBuilder.setContentTitle(appName).setSmallIcon(icon);

        if (intent!=null){
            //从intent中取得apk下载路径
            mUrlStr = intent.getStringExtra("apkUrl");
        }

        InputStream in = null;
        FileOutputStream out = null;
        try {
            //建立下载连接
            URL url = new URL(mUrlStr);
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(false);
            urlConnection.setConnectTimeout(10 * 1000);
            urlConnection.setReadTimeout(10 * 1000);
            urlConnection.setRequestProperty("Connection", "Keep-Alive");
            urlConnection.setRequestProperty("Charset", "UTF-8");
            urlConnection.setRequestProperty("Accept-Encoding", "gzip, deflate");
            urlConnection.connect();

            //以文件流读取数据
            long bytetotal = urlConnection.getContentLength(); //取得文件长度
            long bytesum = 0;
            int byteread = 0;
            in = urlConnection.getInputStream();
            File dir = StorageUtils.getCacheDirectory(this); //取得应用缓存目录
            String apkName = mUrlStr.substring(mUrlStr.lastIndexOf("/") + 1, mUrlStr.length());//取得apK文件名
            File apkFile = new File(dir, apkName);
            out = new FileOutputStream(apkFile);
            byte[] buffer = new byte[BUFFER_SIZE];

            int limit = 0;
            int oldProgress = 0;
            while ((byteread = in.read(buffer)) != -1) {
                bytesum += byteread;
                out.write(buffer, 0, byteread);
                int progress = (int) (bytesum * 100L / bytetotal);
                // 如果进度与之前进度相等，则不更新，如果更新太频繁，则会造成界面卡顿
                if (progress != oldProgress) {
                    updateProgress(progress);
                }
                oldProgress = progress;
            }

            if (oldProgress==100){
//                SPUtil.put(ISDOWNLOAD,false);
            }

            // 下载完成,调用installAPK开始安装文件
//            installApk(apkFile);
//            installProcess(apkFile);
            String apkPath = apkFile.getAbsolutePath();
            EventBus.getDefault().post(new MyEvent("INSTALL_APK",apkPath));

//            mNotifyManager.cancel(NOTIFICATION_ID);

        } catch (Exception e) {
            Log.e(TAG, "download apk file error");
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (IOException ignored) {

                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ignored) {

                }
            }
            mNotifyManager.cancel(NOTIFICATION_ID);
            SPUtil.put(ISDOWNLOAD,false);
//            EventBus.getDefault().post(new MyEvent(Constants.ISDOWNLOAD,false));
        }
    }

    /**
     * 实时更新下载进度条显示
     * @param progress
     */
    private void updateProgress(int progress) {
        //"正在下载:" + progress + "%"
//        mBuilder.setContentText(this.getString(R.string.dialog_choose_update_content, progress)).setProgress(100, progress, false);
        mBuilder.setProgress(100, progress, false);
        mBuilder.setContentText("正在下载:" + progress + "%");
        Log.i("LFX","updateProgress="+progress);
        PendingIntent pendingintent = PendingIntent.getActivity(this, 0, new Intent(), PendingIntent.FLAG_CANCEL_CURRENT);
        mBuilder.setContentIntent(pendingintent);
        mNotifyManager.notify(NOTIFICATION_ID, mBuilder.build());
    }

}
